/* jshint esversion: 6 */
// Command 登陆接口
function Command(Path, User) {
    var $ = this;
    // 参数
    this.Key = User.Key;
    this.CommandKey = ""; // 终端 Key
    this.Path = Path;
    this.onError = console.warn; // 错误输出
    // 打开终端
    this.Run = function () {
        $.Post($.Path + "/Run", function (key) {
            $.CommandKey = key;
        });
    };
    // 执行命令
    this.Exec = function () {
        return new WebSocket($.Path.replace(/https|http/g, "ws") + `/Exec?Key=` + $.Key + "&CommandKey=" + $.CommandKey);
    };
    // 退出终端
    this.Exit = function () {
        $.Post($.Path + "/Exit");
    };
    // 心跳包
    this.HeartBeat = function () {
        $.Post($.Path + "/HeartBeat");
    };
}
// Post Post通信
Command.prototype.Post = function (url, call, data) {
    var $ = this;
    data = data ? data : {};
    data.Key = $.Key;
    if ($.CommandKey != "") {
        data.CommandKey = $.CommandKey;
    }
    // 终端标记
    PetUI.HTTP.Post({
        url: url,
        data: data
    }, function (error, data) {
        if (error != null) {
            $.onError(error);
        } else {
            if (call != undefined) {
                call(data);
            }
        }
    });
};